home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FGFX11.ZIP / EFFECTS.C < prev    next >
Text File  |  1994-04-11  |  22KB  |  654 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. *  EFFECTS.C                                                                 *
  4. *                                                                            *
  5. *  This program demonstrates several methods of fading in an image from an   *
  6. *  off-screen video page using either Fastgraph or Fastgraph/Light.  The set *
  7. *  of routines provided herein are written for 320 x 200 graphics video      *
  8. *  modes, but they could easily be extended to work in other resolutions.    *
  9. *                                                                            *
  10. *  The examples are by no means all inclusive.  Rather, their purpose is to  *
  11. *  illustrate a few methods of creating special effects with Fastgraph or    *
  12. *  Fastgraph/Light.                                                          *
  13. *                                                                            *
  14. *  To compile this program and link it with Fastgraph:                       *
  15. *                                                                            *
  16. *     BCC -ms EFFECTS.C FGS.LIB    (Borland C++)                             *
  17. *                                                                            *
  18. *     CL /AS EFFECTS.C /link FGS   (Microsoft C)                             *
  19. *                                                                            *
  20. *     QCL /AS EFFECTS.C /link FGS  (Microsoft QuickC)                        *
  21. *                                                                            *
  22. *     PC /ms EFFECTS               (Power C)                                 *
  23. *     PCL EFFECTS ;FGS                                                       *
  24. *                                                                            *
  25. *     TCC -ms EFFECTS.C FGS.LIB    (Turbo C and C++)                         *
  26. *                                                                            *
  27. *     ZTC -ms EFFECTS.C FGS.LIB    (Zortech C++)                             *
  28. *                                                                            *
  29. *  This program also can be linked with Fastgraph/Light if you replace the   *
  30. *  FGS library references with FGLS.                                         *
  31. *                                                                            *
  32. *  Fastgraph (tm) and Fastgraph/Light (tm) are graphics libraries published  *
  33. *  by Ted Gruber Software.  For more info, please call, write, or FAX.       *
  34. *                                                                            *
  35. *  Ted Gruber Software                           orders/info (702) 735-1980  *
  36. *  PO Box 13408                                          FAX (702) 735-4603  *
  37. *  Las Vegas, NV  89112                                  BBS (702) 796-7134  *
  38. *                                                                            *
  39. \****************************************************************************/
  40.  
  41. #include <fastgraf.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45.  
  46. /* function prototypes */
  47.  
  48. void main(void);
  49.  
  50. void announce(char *);
  51. int  irandom(int,int);
  52.  
  53. void curtain(int);
  54. void diagonal_fade(int);
  55. void horizontal_random_fade(int);
  56. void inward_tunnel_effect(int);
  57. void outward_tunnel_effect(int);
  58. void spiral_dual(int);
  59. void spiral_layered(int);
  60. void spiral_normal(int);
  61. void split_screen(int);
  62. void unveil(int);
  63. void venetian_blind(int);
  64.  
  65. /* global variables */
  66.  
  67. int delay;
  68. int scroll_delay;
  69.  
  70. /* main program */
  71.  
  72. void main()
  73. {
  74.    int old_mode, new_mode;
  75.    unsigned int count;
  76.    unsigned long start_time;
  77.  
  78.    /* make sure a 320 x 200 color graphics mode is available */
  79.  
  80.    new_mode = fg_bestmode(320,200,2);
  81.    if (new_mode < 0 || new_mode == 12)
  82.    {
  83.       printf("This program requires a 320 x 200 color graphics mode.\n");
  84.       exit(1);
  85.    }
  86.  
  87.    /* determine the number of delay units per half clock tick */
  88.  
  89.    delay = fg_measure() / 2;
  90.  
  91.    /* initialize Fastgraph for the selected video mode */
  92.  
  93.    old_mode = fg_getmode();
  94.    fg_setmode(new_mode);
  95.    fg_allocate(1);
  96.  
  97.    /* display a packed pixel run file on a hidden page */
  98.  
  99.    fg_sethpage(1);
  100.    fg_setpage(1);
  101.    fg_move(0,199);
  102.    fg_dispfile("FG.PPR",320,1);
  103.    fg_setpage(0);
  104.  
  105.    /* compute the number of delay units needed to make the text scroll */
  106.    /* down at the same rate, regardless of the CPU speed or video mode */
  107.  
  108.    count = 0;
  109.    fg_waitfor(1);
  110.    start_time = fg_getclock();
  111.    do
  112.    {
  113.       fg_scroll(0,319,0,7,4,1);
  114.       count++;
  115.    }
  116.    while (fg_getclock() == start_time);
  117.  
  118.    scroll_delay = (delay / 8) - (delay * 2) / count;
  119.    if (scroll_delay < 0) scroll_delay = 0;
  120.  
  121.    /* demonstrate the inward tunnel effect */
  122.  
  123.    announce("inward tunnel effect");
  124.    inward_tunnel_effect(0);
  125.    fg_waitfor(27);
  126.    announce("inward tunnel effect with delay");
  127.    inward_tunnel_effect(delay);
  128.    fg_waitfor(27);
  129.  
  130.    /* demonstrate the outward tunnel effect */
  131.  
  132.    announce("outward tunnel effect");
  133.    outward_tunnel_effect(0);
  134.    fg_waitfor(27);
  135.    announce("outward tunnel effect with delay");
  136.    outward_tunnel_effect(delay);
  137.    fg_waitfor(27);
  138.  
  139.    /* demonstrate the diagonal fade */
  140.  
  141.    announce("diagonal fade");
  142.    diagonal_fade(0);
  143.    fg_waitfor(27);
  144.    announce("diagonal fade with delay");
  145.    diagonal_fade(delay/2);
  146.    fg_waitfor(27);
  147.  
  148.    /* demonstrate the horizontal random fade */
  149.  
  150.    announce("horizontal random fade");
  151.    horizontal_random_fade(delay);
  152.    fg_waitfor(27);
  153.  
  154.    /* demonstrate the curtain effect */
  155.  
  156.    announce("curtain");
  157.    curtain(delay/8);
  158.    fg_waitfor(27);
  159.  
  160.    /* demonstrate the spiral effect */
  161.  
  162.    announce("spiral");
  163.    spiral_normal(delay*2);
  164.    fg_waitfor(27);
  165.  
  166.    /* demonstrate the layered spiral effect */
  167.  
  168.    announce("layered spiral");
  169.    spiral_layered(delay);
  170.    fg_waitfor(27);
  171.  
  172.    /* demonstrate the dual spiral effect */
  173.  
  174.    announce("dual spiral");
  175.    spiral_dual(delay/2);
  176.    fg_waitfor(27);
  177.  
  178.    /* demonstrate the split screen effect */
  179.  
  180.    announce("split screen");
  181.    split_screen(delay/2);
  182.    fg_waitfor(27);
  183.  
  184.    /* demonstrate the unveil effect */
  185.  
  186.    announce("unveil");
  187.    unveil(delay/4);
  188.    fg_waitfor(27);
  189.  
  190.    /* demonstrate the "venetian blind" effect */
  191.  
  192.    announce("venetian blind");
  193.    venetian_blind(delay);
  194.    fg_waitfor(27);
  195.  
  196.    /* restore the original video mode and screen attributes */
  197.  
  198.    fg_freepage(1);
  199.    fg_setmode(old_mode);
  200.    fg_reset();
  201. }
  202.  
  203. /****************************************************************************\
  204. *                                                                            *
  205. *  announce                                                                  *
  206. *                                                                            *
  207. *  Display the name of the special effect we're about to see.                *
  208. *                                                                            *
  209. \****************************************************************************/
  210.  
  211. void announce(message)
  212. char *message;
  213. {
  214.    register int y;
  215.    int length;
  216.  
  217.    /* clear the screen */
  218.  
  219.    fg_erase();
  220.  
  221.    /* display the specified message at the top row */
  222.  
  223.    fg_setcolor(15);
  224.    length = strlen(message);
  225.    fg_locate(0,20-length/2);
  226.    fg_text(message,length);
  227.  
  228.    /* scroll the message to the center of the screen */
  229.  
  230.    fg_setcolor(0);
  231.  
  232.    for (y = 0; y < 96; y+=4)
  233.    {
  234.       fg_scroll(0,319,y,y+7,4,1);
  235.       fg_stall(scroll_delay);
  236.    }
  237.  
  238.    /* wait 1.5 seconds */
  239.  
  240.    fg_waitfor(27);
  241. }
  242.  
  243. /****************************************************************************\
  244. *                                                                            *
  245. *  irandom                                                                   *
  246. *                                                                            *
  247. *  Random number generator used in some of the effects.  It returns an       *
  248. *  integer between min and max inclusive.                                    *
  249. *                                                                            *
  250. \****************************************************************************/
  251.  
  252. int irandom(min,max)
  253. int min, max;
  254. {
  255.    return(rand() % (max-min+1) + min);
  256. }
  257.  
  258. /****************************************************************************\
  259. *                                                                            *
  260. *  curtain                                                                   *
  261. *                                                                            *
  262. *  Reveal each row, one at a time, starting from the bottom and proceeding   *
  263. *  to the top.  This gives the effect of a curtain rising, hence the name.   *
  264. *                                                                            *
  265. \****************************************************************************/
  266.  
  267. void curtain(delay)
  268. int delay;
  269. {
  270.    register int y;
  271.  
  272.    for (y = 199; y >= 0; y--)
  273.    {
  274.       fg_restore(0,319,y,y);
  275.       fg_stall(delay);
  276.    }
  277. }
  278.  
  279. /****************************************************************************\
  280. *                                                                            *
  281. *  diagonal_fade                                                             *
  282. *                                                                            *
  283. *  This reveals the hidden page in two diagonal segments, separated by an    *
  284. *  imaginary line extending from the lower left corner to the upper right    *
  285. *  corner of the screen.  We start with the top line of the left segment and *
  286. *  the bottom line of the right segment, and continue until the entire       *
  287. *  screen is revealed.                                                       *
  288. *                                                                            *
  289. \****************************************************************************/
  290.  
  291. void diagonal_fade(delay)
  292. int delay;
  293. {
  294.    int xmin, xmax;
  295.    int ymin, ymax;
  296.  
  297.    xmin = 0;
  298.    xmax = 319;
  299.    ymin = 0;
  300.    ymax = 199;
  301.  
  302.    while (xmax > 0)
  303.    {
  304.       fg_restore(0,xmax,ymin,ymin+4);
  305.       fg_restore(xmin,319,ymax-4,ymax);
  306.       fg_stall(delay);
  307.  
  308.       xmin += 8;
  309.       xmax -= 8;
  310.       ymin += 5;
  311.       ymax -= 5;
  312.    }
  313. }
  314.  
  315. /****************************************************************************\
  316. *                                                                            *
  317. *  horizontal_random_fade                                                    *
  318. *                                                                            *
  319. *  In this effect, the screen is divided into a series of two-pixel high     *
  320. *  rows.  Each row is revealed in random parts from left to right.  This     *
  321. *  process repeats 20 times, once for each row.  At the end, a call to the   *
  322. *  fg_restore routine guarantees that all rows are transferred.              *
  323. *                                                                            *
  324. \****************************************************************************/
  325.  
  326. void horizontal_random_fade(delay)
  327. int delay;
  328. {
  329.    register int i, j;
  330.    int xwidth;
  331.    int xmin, xmax;
  332.    int y;
  333.    int xpos[100];
  334.  
  335.    for (j = 0; j < 100; j++)
  336.       xpos[j] = 0;
  337.  
  338.    for (i = 0; i < 20; i++)
  339.    {
  340.       for (j = 0; j < 100; j++)
  341.       {
  342.          xmin = xpos[j];
  343.          if (xmin < 320)
  344.          {
  345.             xmax = xmin + irandom(1,10) * 8;
  346.             if (xmax > 320) xmax = 320;
  347.             y = j * 2;
  348.             fg_restore(xmin,xmax-1,y,y+1);
  349.             xpos[j] = xmax;
  350.          }
  351.       }
  352.       fg_stall(delay);
  353.    }
  354.  
  355.    /* make sure we got them all */
  356.  
  357.    fg_restore(0,319,0,199);
  358. }
  359.  
  360. /****************************************************************************\
  361. *                                                                            *
  362. *  inward_tunnel_effect                                                      *
  363. *                                                                            *
  364. *  Starting at the screen edges, reveal the screen through a series of       *
  365. *  concentric hollow rectangles.                                             *
  366. *                                                                            *
  367. \****************************************************************************/
  368.  
  369. void inward_tunnel_effect(delay)
  370. int delay;
  371. {
  372.    int xmin, xmax;
  373.    int ymin, ymax;
  374.  
  375.    xmin = 0;
  376.    xmax = 319;
  377.    ymin = 0;
  378.    ymax = 199;
  379.  
  380.    while (xmin < xmax)
  381.    {
  382.       fg_restore(0,319,ymin,ymin+4);
  383.       fg_restore(xmax-7,xmax,0,199);
  384.       fg_restore(0,319,ymax-4,ymax);
  385.       fg_restore(xmin,xmin+7,0,199);
  386.       fg_stall(delay);
  387.  
  388.       xmin += 8;
  389.       xmax -= 8;
  390.       ymin += 5;
  391.       ymax -= 5;
  392.    }
  393. }
  394.  
  395. /****************************************************************************\
  396. *                                                                            *
  397. *  outward_tunnel_effect                                                     *
  398. *                                                                            *
  399. *  Starting at the screen center, reveal the screen through a series of      *
  400. *  concentric hollow rectangles.                                             *
  401. *                                                                            *
  402. \****************************************************************************/
  403.  
  404. void outward_tunnel_effect(delay)
  405. int delay;
  406. {
  407.    int xmin, xmax;
  408.    int ymin, ymax;
  409.  
  410.    xmin = 152;
  411.    xmax = 167;
  412.    ymin = 95;
  413.    ymax = 104;
  414.  
  415.    while (xmin >= 0)
  416.    {
  417.       fg_restore(xmin,xmax,ymin,ymin+5);
  418.       fg_restore(xmax-7,xmax,ymin,ymax);
  419.       fg_restore(xmin,xmax,ymax-4,ymax);
  420.       fg_restore(xmin,xmin+7,ymin,ymax);
  421.       fg_stall(delay);
  422.  
  423.       xmin -= 8;
  424.       xmax += 8;
  425.       ymin -= 5;
  426.       ymax += 5;
  427.    }
  428. }
  429.  
  430. /****************************************************************************\
  431. *                                                                            *
  432. *  spiral_dual                                                               *
  433. *                                                                            *
  434. *  In this effect, we reveal the screen through two spirals.  One spiral     *
  435. *  emanates clockwise from the screen edges to the screen center, while the  *
  436. *  other emanates counterclockwise from the center to the screen edges.      *
  437. *                                                                            *
  438. \****************************************************************************/
  439.  
  440. void spiral_dual(delay)
  441. int delay;
  442. {
  443.    int xmin_outer, xmax_outer;
  444.    int ymin_outer, ymax_outer;
  445.    int xmin_inner, xmax_inner;
  446.    int ymin_inner, ymax_inner;
  447.  
  448.    xmin_outer = 0;
  449.    xmax_outer = 319;
  450.    ymin_outer = 0;
  451.    ymax_outer = 199;
  452.  
  453.    xmin_inner = 152;
  454.    xmax_inner = 167;
  455.    ymin_inner = 95;
  456.    ymax_inner = 104;
  457.  
  458.    while (xmin_outer < xmin_inner)
  459.    {
  460.       fg_restore(xmin_outer,xmax_outer,ymin_outer,ymin_outer+4);
  461.       fg_stall(delay);
  462.       fg_restore(xmin_inner,xmax_inner,ymax_inner-4,ymax_inner);
  463.       fg_stall(delay);
  464.       fg_restore(xmax_outer-7,xmax_outer,ymin_outer,ymax_outer);
  465.       fg_stall(delay);
  466.       fg_restore(xmax_inner+1,xmax_inner+8,ymin_inner,ymax_inner);
  467.       fg_stall(delay);
  468.       fg_restore(xmin_outer,xmax_outer,ymax_outer-4,ymax_outer);
  469.       fg_stall(delay);
  470.       fg_restore(xmin_inner-8,xmax_inner,ymin_inner,ymin_inner+4);
  471.       fg_stall(delay);
  472.       fg_restore(xmin_outer,xmin_outer+7,ymin_outer,ymax_outer);
  473.       fg_stall(delay);
  474.       fg_restore(xmin_inner-8,xmin_inner-1,ymin_inner,ymax_inner+5);
  475.       fg_stall(delay);
  476.  
  477.       xmin_outer += 8;
  478.       xmax_outer -= 8;
  479.       ymin_outer += 5;
  480.       ymax_outer -= 5;
  481.  
  482.       xmin_inner -= 8;
  483.       xmax_inner += 8;
  484.       ymin_inner -= 5;
  485.       ymax_inner += 5;
  486.    }
  487. }
  488.  
  489. /****************************************************************************\
  490. *                                                                            *
  491. *  spiral_layered                                                            *
  492. *                                                                            *
  493. *  This effect is similar to the normal spiral.  Instead of revealing the    *
  494. *  screen in one iteration, this effect does so in four iterations (layers), *
  495. *  each moving more toward the screen center.                                *
  496. *                                                                            *
  497. \****************************************************************************/
  498.  
  499. void spiral_layered(delay)
  500. int delay;
  501. {
  502.    register int i;
  503.    int xmin, xmax;
  504.    int ymin, ymax;
  505.  
  506.    for (i = 0; i < 4; i++)
  507.    {
  508.       xmin = i * 8;
  509.       xmax = 319 - xmin;
  510.       ymin = i * 5;
  511.       ymax = 199 - ymin;
  512.  
  513.       while (xmin < xmax)
  514.       {
  515.          fg_restore(xmin,xmax,ymin,ymin+4);
  516.          fg_stall(delay);
  517.          fg_restore(xmax-7,xmax,ymin,ymax);
  518.          fg_stall(delay);
  519.          fg_restore(xmin,xmax,ymax-4,ymax);
  520.          fg_stall(delay);
  521.          fg_restore(xmin,xmin+7,ymin,ymax);
  522.          fg_stall(delay);
  523.  
  524.          xmin += 32;
  525.          xmax -= 32;
  526.          ymin += 20;
  527.          ymax -= 20;
  528.       }
  529.    }
  530. }
  531.  
  532. /****************************************************************************\
  533. *                                                                            *
  534. *  spiral_normal                                                             *
  535. *                                                                            *
  536. *  This is a spiral effect in which we reveal the screen as a series of      *
  537. *  rectangles, emanating from the screen edges and proceeding clockwise to   *
  538. *  the center of the screen.                                                 *
  539. *                                                                            *
  540. \****************************************************************************/
  541.  
  542. void spiral_normal(delay)
  543. int delay;
  544. {
  545.    int xmin, xmax;
  546.    int ymin, ymax;
  547.  
  548.    xmin = 0;
  549.    xmax = 319;
  550.    ymin = 0;
  551.    ymax = 199;
  552.  
  553.    while (xmin < xmax)
  554.    {
  555.       fg_restore(xmin,xmax,ymin,ymin+19);
  556.       fg_stall(delay);
  557.       fg_restore(xmax-31,xmax,ymin,ymax);
  558.       fg_stall(delay);
  559.       fg_restore(xmin,xmax,ymax-19,ymax);
  560.       fg_stall(delay);
  561.       fg_restore(xmin,xmin+31,ymin,ymax);
  562.       fg_stall(delay);
  563.  
  564.       xmin += 32;
  565.       xmax -= 32;
  566.       ymin += 20;
  567.       ymax -= 20;
  568.    }
  569. }
  570.  
  571. /****************************************************************************\
  572. *                                                                            *
  573. *  split_screen                                                              *
  574. *                                                                            *
  575. *  Reveal the top half of from left to right while revealing the bottom half *
  576. *  from right to left.                                                       *
  577. *                                                                            *
  578. \****************************************************************************/
  579.  
  580. void split_screen(delay)
  581. int delay;
  582. {
  583.    register int xmin, xmax;
  584.  
  585.    xmin = 0;
  586.    xmax = 319;
  587.  
  588.    while (xmax > 0)
  589.    {
  590.       fg_restore(xmin,xmin+7,0,99);
  591.       fg_restore(xmax-7,xmax,100,199);
  592.       fg_stall(delay);
  593.       xmin += 8;
  594.       xmax -= 8;
  595.    }
  596. }
  597.  
  598. /****************************************************************************\
  599. *                                                                            *
  600. *  unveil                                                                    *
  601. *                                                                            *
  602. *  Starting at the center, reveal the screen in small horizontal increments  *
  603. *  until we reach the left and right edges.                                  *
  604. *                                                                            *
  605. \****************************************************************************/
  606.  
  607. void unveil(delay)
  608. int delay;
  609. {
  610.    register int xmin, xmax;
  611.  
  612.    xmin = 152;
  613.    xmax = 167;
  614.  
  615.    while (xmin >= 0)
  616.    {
  617.       fg_restore(xmin,xmin+7,0,199);
  618.       fg_restore(xmax-7,xmax,0,199);
  619.       fg_stall(delay);
  620.       xmin -= 8;
  621.       xmax += 8;
  622.    }
  623. }
  624.  
  625. /****************************************************************************\
  626. *                                                                            *
  627. *  venetian_blind                                                            *
  628. *                                                                            *
  629. *  Reveal the screen in four iterations, each revealing every fourth row.    *
  630. *  The effect produced resembles opening a Venetian blind.                   *
  631. *                                                                            *
  632. \****************************************************************************/
  633.  
  634. void venetian_blind(delay)
  635. int delay;
  636. {
  637.    register int y;
  638.  
  639.    for (y = 0; y < 200; y += 4)
  640.       fg_restore(0,319,y,y);
  641.    fg_stall(delay);
  642.  
  643.    for (y = 1; y < 200; y += 4)
  644.       fg_restore(0,319,y,y);
  645.    fg_stall(delay);
  646.  
  647.    for (y = 2; y < 200; y += 4)
  648.       fg_restore(0,319,y,y);
  649.    fg_stall(delay);
  650.  
  651.    for (y = 3; y < 200; y += 4)
  652.       fg_restore(0,319,y,y);
  653. }
  654.